package cn.hellotrue;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class HelloTrueNJ {
	public static String baseUrl = "http://112.126.90.197:8888/api/open/";
	public static String traderId = "29";
	public static String bizType = "14";
	public static String sign = "e887d97e573bf5267cb907fba92a0351b2c3dfe271319ed7263d4bf7e9db72db";

	public String getReslutByURL(String url, Map<String, String> maps)
			throws Exception {
//		System.out.println(baseUrl + url);
		HttpPost httpPost = new HttpPost(baseUrl + url);
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		for (String key : maps.keySet()) {
			nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
		}
		httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
		HttpClient httpClient = null;
		HttpResponse response = null;
		HttpEntity entity = null;
		String responseContent = null;
		try {
			httpClient = HttpClients.createDefault();
			response = httpClient.execute(httpPost);
			entity = response.getEntity();
			responseContent = EntityUtils.toString(entity, "UTF-8");

		} catch (Exception e) {
			e.printStackTrace();
		} 

		return responseContent;
	}
	public String getReslutByURL(String url, String data)
			throws Exception {
//		System.out.println(baseUrl + url);
		HttpPost httpPost = new HttpPost(baseUrl + url);
		httpPost.setEntity(new StringEntity(data)); 
		HttpClient httpClient = null;
		HttpResponse response = null;
		HttpEntity entity = null;
		String responseContent = null;
		try {
			// 创建默认的httpClient实例.
			httpClient = HttpClients.createDefault();
			// 执行请求
			response = httpClient.execute(httpPost);
			entity = response.getEntity();
			responseContent = EntityUtils.toString(entity, "UTF-8");

		} catch (Exception e) {
			e.printStackTrace();
		} 

		return responseContent;
	}
	public String getPhone(String phone) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String getPhoneUrl = "getPhone";
		map.put("traderId", traderId);
		map.put("phone", phone);
		map.put("releaseType", "1");
		map.put("bizType", bizType);
		map.put("key", sign);
		//String data = "traderId=29&phone="+phone+"&releaseType=1&bizType=14&key=e887d97e573bf5267cb907fba92a0351b2c3dfe271319ed7263d4bf7e9db72db";

		//System.out.println(data);
		String result = getReslutByURL(getPhoneUrl, map);

		return result;
	}

	public String getMessage(String orderId) throws Exception {
		String getMessageUrl = "getMessage";
		Map<String, String> map = new HashMap<String, String>();
		map.put("traderId", traderId);
		map.put("orderId", orderId);
		map.put("key", sign);
		String result = "";
		// {"status":"error","errorMsg":"暂时获取不到验证码","errorCode":null,"module":null}
		for (int i = 0; i < 20; i++) {
			result = getReslutByURL(getMessageUrl, map);
			if ("".equals(result) || result.contains("暂时")) {
				try {
					result = "超时";
					Thread.sleep(3000);
				} catch (InterruptedException e) {
				}
			} else {
				break;
			}
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
		HelloTrueNJ ht = new HelloTrueNJ();
		String phoneResult = "";
		String orderId = "";
		String messageResult = "";
		String checkCode = "";
		while(true){
			phoneResult = ht.getPhone("15950517423");
			System.out.println(phoneResult);
			if(phoneResult.contains("稍后再试")){
				Thread.sleep(3000);
			}else if(phoneResult.contains("无法锁定")){
				return;
			}else{
				JSONObject phoneJ = JSONObject.parseObject(phoneResult);
				if ("error".equals(phoneJ.getString("status")))
					return;
				// {"status":"success","errorMsg":null,"errorCode":null,"module":{"bizOrderId":"989059","phone":"13405832132"}}
				orderId = phoneJ.getJSONObject("module").getString("bizOrderId");
				break;
			}
		}
		while(true){
			messageResult = ht.getMessage(orderId);
			System.out.println(messageResult);
			if(phoneResult.contains("稍后再试")){
				Thread.sleep(3000);
			}else if(phoneResult.contains("无法锁定")){
				return;
			}else{
				JSONObject messageJ = JSONObject.parseObject(messageResult);
				// {"status":"success","errorMsg":null,"errorCode":null,"module":{"text":null,"checkCode":"345345"}}
				
				checkCode = messageJ.getJSONObject("module").getString("checkCode");
				System.out.println("获取到的验证码信息为：" + checkCode);
				break;
			}
		}
	}
}
